package com.example.lin.zyfypt_327ljd.model;

import com.example.lin.zyfypt_327ljd.bean.ProjectBean;
import com.example.lin.zyfypt_327ljd.iface.ProjectListener;
import com.example.lin.zyfypt_327ljd.iface.Projectiface;
import com.example.lin.zyfypt_327ljd.iface.TcaseListener;
import com.example.lin.zyfypt_327ljd.service.ProjectService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

;

/**
 * Created by lin on 2017/4/20.
 */

//model 获取案例数据
public class ProjectModel implements Projectiface{
    private Retrofit retrofit;
    private  String BASEURL
            ="http://amicool.neusoft.edu.cn/";
    //构造函数
    public ProjectModel()
    {
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    //获取文章数据
    public void getResultList(String mod, int page, String sessionID,final ProjectListener projectListener) {
        //使用Retrofit---2
        ProjectService projectService
                =retrofit.create(ProjectService.class);
        Call<List<ProjectBean>> call
                =projectService.getResult(mod,page,sessionID);
        //使用Retrofit---3
        call.enqueue(new Callback<List<ProjectBean>>() {
            @Override
            public void onResponse(Call<List<ProjectBean>> call, Response<List<ProjectBean>> response) {
                if(response.isSuccessful() && response!=null){
                    projectListener.onResponse(response.body());
                }
                else projectListener.onFail("onresponse fail");
            }
            @Override
            public void onFailure(Call<List<ProjectBean>> call, Throwable t) {
                projectListener.onFail(t.toString());
            }
        });
    }
}
